package com.sx.shop.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//md5加密
public class MD5Util {

    /**
     * @param str 加密前的参数
     * @return　md5加密之后的值
     */
    public static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes("UTF-8"));
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            throw new IllegalArgumentException("加密失败");
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String a = "123";
        System.out.println(md5(a));
    }
}
